/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss;

import java.io.StringReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.jinhe.tss.core.exception.BusinessException;

public class ImportPMS {
    
    Connection old_conn = Helper.getOracleConnection(Helper.pms_db[0], Helper.pms_db[1], Helper.pms_db[2]);
    Connection new_conn = Helper.getOracleConnection(Helper.tss_db[0], Helper.tss_db[1], Helper.tss_db[2]);
    
    public static void main(String[] args){
        ImportPMS imp = new ImportPMS();
        imp.importLongType();
    }
    
    /**
     * 导入门户表、门户元素表大字段类型字段数据
     */
    public void importLongType(){
        Statement old_stmt = null;
        Statement new_stmt = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            old_stmt = old_conn.createStatement();
            new_stmt = new_conn.createStatement();
            
            // 布局器
            pstmt = new_conn.prepareStatement("update pms3.pms_layout t set t.definition = ? where t.id = ?");
            rs = old_stmt.executeQuery("select t.id, t.definition from test.layout t where t.id > 2 order by t.groupId, t.seqNo");
            while (rs.next()) {
                long id = rs.getLong("id");
                String longStr = rs.getString("definition");
                if(longStr == null)
                    continue;
                
                pstmt.setCharacterStream(1, new StringReader(longStr), longStr.length());
                pstmt.setLong(2, id);
                
                pstmt.executeUpdate();
            }
            pstmt.close();
            rs.close();   
            
            // 修饰器
            pstmt = new_conn.prepareStatement("update pms3.pms_decorator t set t.definition = ? where t.id = ?");
            rs = old_stmt.executeQuery("select t.id, t.definition from test.decorator t where t.id > 4 order by t.groupId, t.seqNo");
            while (rs.next()) {
                long id = rs.getLong("id");
                String longStr = rs.getString("definition");
                if(longStr == null)
                    continue;
                
                pstmt.setCharacterStream(1, new StringReader(longStr), longStr.length());
                pstmt.setLong(2, id);
                
                pstmt.executeUpdate();
            }
            pstmt.close();
            rs.close();   
            
            // portlet
            pstmt = new_conn.prepareStatement("update pms3.pms_portlet t set t.definition = ? where t.id = ?");
            rs = old_stmt.executeQuery("select t.id, t.definition from test.portlet t order by t.groupId, t.seqNo");
            while (rs.next()) {
                long id = rs.getLong("id");
                String longStr = rs.getString("definition");

                pstmt.setCharacterStream(1, new StringReader(longStr), longStr.length());
                pstmt.setLong(2, id);
                if(longStr == null)
                    continue;
                
                pstmt.executeUpdate();
            }
            pstmt.close();
            rs.close();   
            
            // 门户结构
            pstmt = new_conn.prepareStatement("update pms3.pms_portalStructure t set t.supplement = ? where t.id = ?");
            rs = old_stmt.executeQuery("select t.id, t.supplement from test.portalStructure t order by t.parentId, t.seqNo");
            while (rs.next()) {
                long id = rs.getLong("id");
                String longStr = rs.getString("supplement");
                if(longStr == null)
                    continue;
                
                pstmt.setCharacterStream(1, new StringReader(longStr), longStr.length());
                pstmt.setLong(2, id);
                
                pstmt.executeUpdate();
            }
            pstmt.close();
            rs.close();   
            
            old_stmt.close();   
            new_stmt.close();   
        } catch (Exception e) {
            throw new BusinessException("错误！", e);
        }finally{
            System.exit(0);
        }
    }
}

